package org.prail.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;

import org.prail.net.Configuration;
import org.prail.settings.Settings;
import org.prail.threads.LoginThread;
import org.prail.ui.utils.UIUtils;

public class LoginFrame extends javax.swing.JFrame {
	private static final long serialVersionUID = -7658945341008882339L;

	public LoginFrame() {
		initComponents();
	}
	
	public void login() {
		setLoginEnabled(false);
		String username = "";
		String password = "";
		boolean save = false;
		if(jCheckBox1.isSelected()) {
			username = jTextField1.getText();
		}
		if(jCheckBox2.isSelected()) {
			password = new String(jPasswordField1.getPassword());
		}
		if(jCheckBox3.isSelected()) {
			save = true;
		}
		Settings.saveSettings(username, password, save, Configuration.saveDir);
		Configuration.username = jTextField1.getText();
		Configuration.password = new String(jPasswordField1.getPassword());
		final Thread login = new LoginThread(this);
		login.start();
	}
	
	public void setLoginEnabled(boolean enable) {
		jCheckBox1.setEnabled(enable);
		jCheckBox2.setEnabled(enable);
		jCheckBox3.setEnabled(enable);
		jTextField1.setEnabled(enable);
		jPasswordField1.setEnabled(enable);
		jButton1.setEnabled(enable);
	}

	private void initComponents() {
		setIcon();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jPasswordField1 = new javax.swing.JPasswordField();
		jCheckBox1 = new javax.swing.JCheckBox();
		jCheckBox2 = new javax.swing.JCheckBox();
		jCheckBox3 = new javax.swing.JCheckBox();
		jButton1 = new javax.swing.JButton();
		jLabel4 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Login VK.com");
		setResizable(false);

		jLabel1.setText("Phone or email:");

		jLabel2.setText("Password:");

		jCheckBox1.setText("Save username");
		jCheckBox1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(jCheckBox1.isSelected()) {
					jCheckBox2.setEnabled(true);
				} else {
					jCheckBox2.setEnabled(false);
					jCheckBox2.setSelected(false);
					jCheckBox3.setEnabled(false);
					jCheckBox3.setSelected(false);
				}
				
			}
			
		});

		jCheckBox2.setText("Save password");
		jCheckBox2.setEnabled(false);
		jCheckBox2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(jCheckBox2.isSelected()) {
					jCheckBox3.setEnabled(true);
				} else {
					jCheckBox3.setEnabled(false);
					jCheckBox3.setSelected(false);
				}
				
			}
			
		});

		jCheckBox3.setText("Auto login");
		jCheckBox3.setEnabled(false);

		jButton1.setText("Login");
		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				login();
			}
			
		});

		jLabel4.setText("Note: an account on vk.com is required to use this program.");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(129, 129, 129)
								.addComponent(jLabel2)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addComponent(jLabel1)
								.addGap(116, 116, 116))
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGap(0,
																		0,
																		Short.MAX_VALUE)
																.addComponent(
																		jButton1))
												.addComponent(jPasswordField1)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jCheckBox3)
																				.addComponent(
																						jCheckBox2)
																				.addComponent(
																						jCheckBox1)
																				.addComponent(
																						jLabel4))
																.addGap(0,
																		0,
																		Short.MAX_VALUE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(14, 14, 14)
								.addComponent(jLabel1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jTextField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(7, 7, 7)
								.addComponent(jLabel2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPasswordField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jCheckBox1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jCheckBox2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jCheckBox3)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(jButton1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jLabel4).addContainerGap()));

		jLabel1.getAccessibleContext().setAccessibleName("Email/:");

		if(Configuration.username != null) {
			jTextField1.setText(Configuration.username);
			jCheckBox1.setSelected(true);
		}
		if(Configuration.password != null) {
			jPasswordField1.setText(Configuration.password);
			jCheckBox2.setEnabled(true);
			jCheckBox3.setEnabled(true);
			jCheckBox2.setSelected(true);
			jCheckBox3.setSelected(Configuration.autoLogin);
		}
		UIUtils.center(this, 314, 272);
		if(Configuration.autoLogin) {
			login();
		}
	}
	
	public void setIcon() {
		try {
			setIconImage(ImageIO.read(getClass().getResource(
					"/org/prail/images/icon.png")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private javax.swing.JButton jButton1;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JCheckBox jCheckBox2;
	private javax.swing.JCheckBox jCheckBox3;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPasswordField jPasswordField1;
	private javax.swing.JTextField jTextField1;
}
